<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>js原型链之原型继承示例</title>
  <style>
    .tabs {
      display: flex;
      justify-content: center;
    }
    main {
      display: inline-block;
      width: 400px;
      margin: 0 auto;
      text-align: center;
    }
    nav {
      display: flex;
      justify-content: center;
    }
    nav .nav-item {
      flex: 1;
      display: inline-block;
      text-decoration: none;
      width: 100px;
      margin-right: 1px;
      padding: 5px 0;
      cursor: pointer;
      text-align: center;
      background-color: #f0c40f;
      color: #666;
    }
    nav .nav-item:last-child {
      margin-right: 0;
    }
    section {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 150px;
      background-color: #9b59b6;
      margin: 1px 0;
      font-size: 36px;
      font-weight: bold;
      color: #fff;
    }
  </style>
</head>
<body>
  <div class="tabs">
    <main id="tab1">
      <nav>
        <a href="#" class="nav-item">JS</a>
        <a href="#" class="nav-item">TS</a>
        <a href="#" class="nav-item">ES</a>
      </nav>
      <section>1</section>
      <section style="display: none">2</section>
      <section style="display: none">3</section>
    </main>
    <main id="tab2">
      <nav>
        <span class="nav-item">Angular</span>
        <span class="nav-item">Vue</span>
        <span class="nav-item">React</span>
      </nav>
      <section>1</section>
      <section style="display: none">2</section>
      <section style="display: none">3</section>
    </main>
  </div>
  <script>
    function extend(sub, sup) {
      sub.prototype = Object.create(sup.prototype)
      Object.defineProperty(sub.prototype, 'constructor', {
        value: sub,
        enumerable: false
      })
    }
    // Animation
    function Animation() {}
    Animation.prototype.show = function() {
      this.style.display = 'flex'
    }
    Animation.prototype.hide = function() {
      this.style.display = 'none'
    }
    Animation.prototype.background = function(color) {
      this.style.backgroundColor = color
      this.style.color = '#fff'
    }
    // Tab
    function Tab(el, { link = 'a', section = 'section', callback = '' } = {}) {
      this.tab = document.querySelector(el)
      this.links = this.tab.querySelectorAll(link)
      this.sections = this.tab.querySelectorAll(section)
      this.callback = callback
    }
    extend(Tab, Animation)
    Tab.prototype.run = function() {
      this.bindEvent()
      this.reset()
      this.action(0)
    }
    Tab.prototype.bindEvent = function() {
      this.links.forEach((item, index) => {
        item.addEventListener('click', () => {
          this.reset()
          this.action(index)
          if (this.callback) {
            this.callback(item)
          }
        })
      })
    }
    Tab.prototype.reset = function() {
      this.links.forEach((item, index) => {
        this.background.call(this.links[index], '#f0c40f')
        this.hide.call(this.sections[index])
      })
    }
    Tab.prototype.action = function(index) {
      this.background.call(this.links[index], '#9b59b6')
      this.show.call(this.sections[index])
    }
    new Tab('#tab1').run()
    new Tab('#tab2', { link: 'span', callback(item) {
        console.log(`点击${item.innerHTML}`)
      }
    }).run()
  </script>
</body>
</html>